Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
偷懒的办法,直接正则表达式甩进去。
import java.util.regex.*;
public class Solution {
public int strStr(String haystack, String needle) {
Pattern p = Pattern.compile(needle);
Matcher m = p.matcher(haystack);
return (m.find())? m.start() : -1;
}
}
看来正则表达式的效率比较捉急。
String#equals()
方法比较字符串public class Solution {
public int strStr(String haystack, String needle) {
if (haystack.isEmpty() && needle.isEmpty()) { return 0; }
for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {
if (haystack.substring(i,i+needle.length()).equals(needle)) { return i; }
}
return -1;
}
}
不错。
char
地比较public class Solution {
public int strStr(String haystack, String needle) {
if (haystack.isEmpty() && needle.isEmpty()) { return 0; } // 唯一的特殊情况
char[] stackArray = haystack.toCharArray();
char[] needleArray = needle.toCharArray();
for (int i = 0; i < stackArray.length - needleArray.length + 1; i++) {
int cursorNeedle = 0;
while (cursorNeedle < needleArray.length && stackArray[i+cursorNeedle] == needleArray[cursorNeedle]) {
cursorNeedle++;
}
if (cursorNeedle == needleArray.length) { return i; } // edge case needle.length() == 0 会从这里跳出。
}
return -1; // edge case haystack.length() == 0 不会进入for loop,直接到这里。
}
}
和用String#equals()
效率差不多。这说明,Java的String#equals()
方法的复杂度还是线性的,没有什么黑魔法。